package cn.com.homeyoda.aimate.aihealth.ui.kepu;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.stx.xhb.xbanner.XBanner;
import com.stx.xhb.xbanner.transformers.Transformer;
import com.wzq.mvvmsmart.utils.GlideLoadUtils;

import java.util.ArrayList;
import java.util.List;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBannerBean;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBannerList;
import cn.com.homeyoda.aimate.aihealth.bean.KePuBean;
import cn.com.homeyoda.aimate.aihealth.bean.MultipleItem;
import cn.com.homeyoda.aimate.aihealth.bean.PopularListBean;
import cn.com.homeyoda.aimate.aihealth.ui.detail.DetailActivity;

import static com.wzq.mvvmsmart.utils.Utils.getContext;

public class KePuAdapter extends BaseMultiItemQuickAdapter<MultipleItem<Object>, BaseViewHolder> {

    private final Context mContext;
    private final ArrayList<KePuBannerBean> bannerList = new ArrayList<>();
    List<KePuBannerList.DataBean> bannerBean = new ArrayList<>();
    public KePuAdapter(Context context) {
        super(new ArrayList<>());
        this.mContext = context;
        // 绑定类型与布局ID
        addItemType(MultipleItem.KP_BANNER, R.layout.kepu_banner);
        addItemType(MultipleItem.KP_NORMAL, R.layout.kepu_normal_item);
//        bannerList.add(new KePuBannerBean("https://img0.baidu.com/it/u=468397402,950855054&fm=253&fmt=auto?w=1400&h=800", "二手烟只是害人病1"));
//        bannerList.add(new KePuBannerBean("https://img2.baidu.com/it/u=647265987,1253183144&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500", "二手烟只是害人病2"));
//        bannerList.add(new KePuBannerBean("https://img0.baidu.com/it/u=3217812679,2585737758&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "二手烟只是害人病3"));
//        bannerList.add(new KePuBannerBean("https://img0.baidu.com/it/u=3429084431,3824519370&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "二手烟只是害人病4"));
//        bannerList.add(new KePuBannerBean("https://img0.baidu.com/it/u=694832972,884680727&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800", "二手烟只是害人病5"));
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, MultipleItem item) {
        PopularListBean.RowsBean kePu = (PopularListBean.RowsBean) item.getData();
        int type = item.getItemType();
        if (type == MultipleItem.KP_NORMAL) {
            holder.setText(R.id.tv_title, kePu.getTitle());
            ImageView imageView = holder.getView(R.id.iv_img);
            //holder.setImageResource(R.id.iv_img, R.mipmap.bg_kepu_normal);
            GlideLoadUtils.loadRoundCornerImg(imageView,kePu.getCoverImage(),R.mipmap.ic_kepu1,5);
            holder.setText(R.id.tv_author, "发布人: " + kePu.getReleasePeopleId());
            holder.setText(R.id.tv_read, kePu.getBrowseCount() + "");
            String time = kePu.getReleaseTime().split(" ")[0]; // 按空格分割，取第一部分
            holder.setText(R.id.tv_date, time);
        } else {
            XBanner banner = holder.getView(R.id.kepu_banner);
            banner.setBannerData(bannerList);
            banner.loadImage((banner12, model, view, position) -> {
                KePuBannerBean bannerBean = (KePuBannerBean) model;
                Glide.with(mContext)
                        .load(bannerBean.getXBannerUrl())
                        .apply(RequestOptions.bitmapTransform(new RoundedCorners(16)))
                        .into((ImageView) view);
            });
            banner.setPageTransformer(Transformer.Default);
            banner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                @Override
                public void onItemClick(XBanner banner, Object model, View view, int position) {
                    Log.i("huage","position=banner=="+bannerBean.get(position).getTitle());
//                    mContext.startActivity(new Intent(mContext, DetailActivity.class));
                    Intent intent = new Intent(getContext(), DetailActivity.class);
                    intent.putExtra("id", bannerBean.get(position).getPopularId());
                    mContext.startActivity(intent);
                }
            });

        }
    }

    public void setBannerList(List<KePuBannerList.DataBean> dataBean){
        bannerBean.clear();
        bannerBean.addAll(dataBean);
        bannerList.clear();
        for(KePuBannerList.DataBean dataBean1 : bannerBean){
            KePuBannerBean kePuBannerBean = new KePuBannerBean(dataBean1.getImageUrl(),dataBean1.getTitle());
            bannerList.add(kePuBannerBean);
        }
        notifyDataSetChanged();
    }
}
